package com.cloudptt.wp.ui.call;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudptt.api.product.NetworkListener;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.adapter.ICallInfoDesc;
import com.cloudptt.api.product.func.sys.SysStateInfo;
import com.cloudptt.api.product.func.sys.listener.SysListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;

/**
 * Created by dell on 2018/4/16.
 */

public class AudioRecorderDlg implements ICallInfoDesc, NetworkListener, SysListener {
    private static final String TAG = "FloorDlg";
    private Dialog mDialog;
    private TextView floorTv =null;
    private TextView secTv =null;
    private Button endBtn = null;
    public static boolean isShow = false;

    private Context mContext;

    public AudioRecorderDlg(Context mContext) {
        this.mContext = mContext;
    }
    /**
     * 显示对话框
     */
    public void  showRecordeingDialog(){
        mDialog = new Dialog(mContext, R.style.Theme_Dialog);
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(mContext.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.dialog_floor, null);
        mDialog.setContentView(v);
        //获取控件
        floorTv = (TextView) v.findViewById(R.id.floorTv);
        secTv = (TextView) v.findViewById(R.id.secTv);
        endBtn = (Button)v.findViewById(R.id.endBtn);
        SysApplication app = ((SysApplication) mContext.getApplicationContext());
        Product product = app.getProduct();
        endBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(product != null)
                {
                    product.setCurGroupInfo(null);
                }
                dimissDialog();
            }
        });

        if(product != null)
        {
            product.setNetworkListener(this);
            product.setSysListener(this);
            product.getGCallEventAdapter().setCallInfoDesc(this);
        }
        //显示
        Window win = mDialog.getWindow();
        if(win != null
        		&& mDialog != null)
        {
        	WindowManager.LayoutParams lp = win.getAttributes();
        	lp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        	lp.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        	lp.gravity = Gravity.CENTER;
        	win.setAttributes(lp);
        	mDialog.show();
        }
       
    }

    /**
     * 取消对话框
     */
    public void dimissDialog(){
        if (mDialog != null && mDialog.isShowing()) {
           mDialog.dismiss();
           mDialog=null;
        }
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.d(TAG, "onKeyDown keyCode "+ keyCode);

        SysApplication app = ((SysApplication) mContext.getApplicationContext());
        Product product = app.getProduct();
        if (keyCode == KeyEvent.KEYCODE_SOFT_LEFT
                || keyCode == 66
                || keyCode == 1
                ||keyCode == KeyEvent.KEYCODE_BACK)
        {
            if(product != null)
            {
                product.setCurGroupInfo(null);
            }
            isShow = false;
            dimissDialog();
        }
        return true;
    }

    @Override
    public boolean isLogined() {
        return false;
    }

    @Override
    public void networkChange(boolean isConnect) {

    }

    @Override
    public void updateCallUser(String callKey, boolean isShow, String userDesc) {
        Log.d(TAG, "updateCallUser callKey = " + callKey + " isShow = " + isShow + " userDesc = " + userDesc);
    }

    @Override
    public void updateCallInfoDesc(String callKey, String gNum, String callInfoDesc) {
        Log.d(TAG, "updateCallUser callKey = " + callKey + " gNum = " + gNum + " callInfoDesc = " + callInfoDesc);
        floorTv.setText(callInfoDesc);
    }

    @Override
    public void updateSecDesc(String callKey, String secStr) {
        Log.d(TAG, "updateCallUser callKey = " + callKey + " secStr = " + secStr);
        secTv.setText(secStr);
    }

    @Override
    public void onSysState(SysStateInfo stateInfo) {
        Log.d(TAG, "onSysState stateInfo = " + stateInfo);
        int state = stateInfo.getState();
//		int cause = stateInfo.getCause();
        if(SysStateInfo.SysState_SipAuthSucState != state)
        {
            dimissDialog();
        }
    }

    @Override
    public void onSipAuthResult(int code) {

    }

    @Override
    public void onForceDetach() {

    }

    @Override
    public void onKeepAlive(boolean isconnected) {

    }

    @Override
    public void onSvrRestart() {

    }
}
